package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"strings"

	"github.com/gin-gonic/gin"
)

// PageSysContent 获取所有文章
func PageSysContent(c *gin.Context) {

	/*
		Lang    string `json:"lang"`
		Title   string `json:"title"`
		Type    string `json:"type"`
		Content string `json:"content"`
		Status  uint8  `json:"status"`
	*/
	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Lang     string `json:"lang" form:"lang"`
		Type     string `json:"type" form:"type"`
		Status   string `json:"status" form:"status"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	// log.Printf("PageUser: %+v", dataRequest)

	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " `title` like " + keyword
	}
	if dataRequest.Lang != "" {
		if where != "" {
			where += " and "
		}
		where += " lang = " + dataRequest.Lang
	}
	if dataRequest.Type != "" {
		if where != "" {
			where += " and "
		}
		where += " type = " + dataRequest.Type
	}
	if dataRequest.Status != "" {
		if where != "" {
			where += " and "
		}
		where += " status = " + dataRequest.Status
	}

	list, page, total, err := services.NewSysContentService(utils.MysqlClient).PageSysContentList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetUser 获取文章
func GetSysContent(c *gin.Context) {

	var dataRequest struct {
		ID string `json:"id" form:"id"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.ID != "" {
		if where != "" {
			where += " and "
		}
		where += " id = " + dataRequest.ID
	}
	data, err := services.NewSysContentService(utils.MysqlClient).GetSysContent(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// CreateUser 创建文章
func CreateSysContent(c *gin.Context) {

	/**
	Lang    string `json:"lang"`
	Title   string `json:"title"`
	Type    string `json:"type"`
	Content string `json:"content"`
	Status  uint8  `json:"status"`
	AppID   int    `json:"app_id"`
	*/
	var createRequest struct {
		Lang    string `json:"lang" form:"lang"`
		Title   string `json:"title" form:"title"`
		Type    string `json:"type" form:"type"`
		Content string `json:"content" form:"content"`
		Status  uint8  `json:"status" form:"status"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysContentCreate{
		Lang:    createRequest.Lang,
		Title:   createRequest.Title,
		Type:    createRequest.Type,
		Content: createRequest.Content,
		Status:  createRequest.Status,
	}
	data, err := services.NewSysContentService(utils.MysqlClient).CreateSysContent(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新文章
func UpdateSysContent(c *gin.Context) {
	var createRequest struct {
		ID      uint   `json:"id" form:"id"`
		Lang    string `json:"lang" form:"lang"`
		Title   string `json:"title" form:"title"`
		Type    string `json:"type" form:"type"`
		Content string `json:"content" form:"content"`
		Status  uint8  `json:"status" form:"status"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysContentUpdate{
		ID:      createRequest.ID,
		Lang:    createRequest.Lang,
		Title:   createRequest.Title,
		Type:    createRequest.Type,
		Content: createRequest.Content,
		Status:  createRequest.Status,
	}
	data, err := services.NewSysContentService(utils.MysqlClient).UpdateSysContent(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除文章
func DeleteSysContent(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewSysContentService(utils.MysqlClient).DeleteSysContent(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewSysContentService(utils.MysqlClient).DeleteSysContents(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除文章的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete data"})
}
